package com.marvels.approve.process.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.manager.dto.OpinionDTO;
import com.marvels.approve.process.dto.HandleProcessDTO;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.QueryStatusNumDTO;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.common.constants.CaseNodeConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**流程处理
 * @author hejie
 */
@RestController
@RequestMapping("approve/process/service")
public class ProcessController {
	@Autowired
	private IProcessService processService;
	/**单子状态
	 * @return
	 */
	@RequestMapping("pstatus")
	@ResponseBody
	public MarvelsResultDTO processStatus(){
		try {
			
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
//		} catch (MarvelsBusinessException e) {
//			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
//			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	/**查看取件列表
	 * @return
	 */
	@RequestMapping("plist")
	@ResponseBody
	public MarvelsResultDTO processList(){
		try {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
//		} catch (MarvelsBusinessException e) {
//			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
//			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**查询处理单状态数量
	 * @author 何杰
	 * 2018年4月12日
	 * @return
	 */
	@RequestMapping("query_status_num")
	@ResponseBody
	public MarvelsResultDTO queryStatusNum(){
		try {
			List<String> queryNodes = new ArrayList();
			queryNodes.add(CaseNodeConstant.WOCHECKING.getCode());
			queryNodes.add(CaseNodeConstant.WOAPPROVAL.getCode());
			queryNodes.add(CaseNodeConstant.WOSAMPLING.getCode()); 
			HandleProcessDTO dto = new HandleProcessDTO();
			dto.setQueryNodes(queryNodes);
			QueryStatusNumDTO qsNum = null;
			qsNum = this.processService.queryStatusNum(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), qsNum);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	/**取件
	 * @author 何杰
	 * 2018年4月16日
	 * @return
	 */
	@RequestMapping("handle_process")
	@ResponseBody
	public MarvelsResultDTO handleProcess(){
		try {
			List<String> queryNodes = new ArrayList();
			queryNodes.add(CaseNodeConstant.WOCHECKING.getCode());
			queryNodes.add(CaseNodeConstant.WOAPPROVAL.getCode());
			queryNodes.add(CaseNodeConstant.WOSAMPLING.getCode()); 
			HandleProcessDTO dto = new HandleProcessDTO();
			dto.setQueryNodes(queryNodes);
			LoanApplyCaseDTO loanApplyCase = this.processService.handleZSJProcess(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCase);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	
	/**
	 * 定时签约
	 */
	@RequestMapping("timingLoanCompactSign")
	@ResponseBody
	public void timingLoanCompactSign(){
		MarvelsLogUtil.getInstance().info(">>>>>>financial定时签约开始");
		processService.timingLoanCompactSign();
		MarvelsLogUtil.getInstance().info(">>>>>>financial定时签约结束");
	}
	
}
